package teatime.core.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import teatime.core.service.manager.ResourceManager;



public class ResourceManagerImpl implements ResourceManager {
	private Map<String, String> resourceMap;
	private static Properties properties = null;

	public Map<String, String> getResourceMap() throws IOException {
		if (null == resourceMap) {
			setResourceMap(getResourceMap("applicationResources.properties"));
		}
		return resourceMap;
	}

	private Map<String, String> getResourceMap(String resourceFileName)
			throws IOException {
		Map<String, String> resourceMap = new HashMap<String, String>();
		if (properties == null) {
			properties = new Properties();
			ClassLoader cl = Thread.currentThread().getContextClassLoader();
			InputStream inputStream = null;
			if (cl != null) {
				inputStream = cl.getResourceAsStream(resourceFileName);
			}
			if (inputStream != null) {
				properties.load(inputStream);
				inputStream.close();
			}
			for (Iterator<Object> it = properties.keySet().iterator();it.hasNext();) {
				String key = it.next().toString();
				String value = properties.getProperty(key);
				resourceMap.put(key, value);
			}
		}
		return resourceMap;
	}

	public void setResourceMap(Map<String, String> resourceMap) {
		this.resourceMap = resourceMap;
	}

}
